<template>
    <div>
        <van-nav-bar 
        title="用户注册"
        left-text="返回"
        left-arrow
        @click-left="goBack"
        />
        <div class="register-panel">
            <van-field 
            v-model="username"
            label="用户名"
            icon="clear"
            placeholder="请输入用户名"
            required
            @click-icon="username = ''"
             :error-message="usernameErrorMsg"
            />
             <van-field 
            v-model="password"
            type="password"
            lable="密码"
            placeholder="请输入密码"
            required
            :error-message="passwordErrorMsg"
            />
        </div>
        <div class="register-button">
            <van-button type="primary" size="large" @click="registerAction" :loading="openLoading">马上注册</van-button>
        </div>
    </div>
</template>
<script>
    import axios from 'axios'
    //引入接口文件
    import url from '@/serviceAPI.config.js'
    //引入弹出组件
    import {Toast} from 'vant'
    export default {
        data(){
            return{
                username:'',
                password:'',
                //是否开启用户的Loading
                openLoading:false,
                usernameErrorMsg:'',
                passwordErrorMsg:''
            }
        },

        //跳转
        methods:{
            //包括表单验证
            registerAction(){
                this.checkForm() && this.axiosRegisterUser()
            },
            axiosRegisterUser(){
                //先把按钮进行loading状态，防止重复提交
                this.openLoading=true;
                axios({
                    url:url.registerUser,
                    method:'post',
                    data:{
                        username:this.username,
                        password:this.password
                    }
                }).then(response=>{
                   // console.log(response)
                    //如果返回code为200，代表注册成功，给toast提示
                    if(response.data.code==200){
                        Toast.success('亲，注册成功啦😊')
                        this.$router.push('/')
                    }else{
                        console.log(response.data.message)
                         this.openLoading=false
                        Toast.fail('亲，注册失败😭')
                    
                    }
                    //console.log(response.data.code)
                }).catch((error)=>{
                    //console.log(error)
                    Toast.fail('亲，注册失败😭😭')
                    this.openLoading=false
                })
            },
            goBack(){
                this.$router.go(-1)
            },
            //验证表单信息
            checkForm(){
                let isOk = true
                if(this.username.length<5){
                    this.usernameErrorMsg = "亲，用户名不能小于5位"
                    isOk = false
                }else{
                    this.usernameErrorMsg=''
                }
                if(this.password.length<6){
                    this.passwordErrorMsg = '亲，密码不能少于6位'
                    isOk = false
                }else{
                    this.passwordErrorMsg = ''
                }
                return isOk
            }
        }
    }
</script>
<style scoped>
    .register-panel{
        width:96%;
        border-radius: 5px;
        margin:20px auto;
        padding-bottom:50px;
    }
    .register-button{
        padding-top:10px;
    }
</style>

